<?php
namespace app\admin\controller;

use app\model\admin;
use think\Request;
use think\Session;
use think\Controller;

class Page extends Controller
{
    //单页列表
    public function index()
    {
        $dao = new \app\model\admin\Page();
        $where = [];//自 5.4 起可以使用短数组定义语法，用 [] 替代 array()
        $title = $this->request->get("title");
        $lists = $dao->get_page_lists($where,$sort='id desc',$paginate=10);
        $this->assign('lists', $lists);
        return $this->fetch('page/index');
    }

    //单页详情
    public function detail()
    {
        $dao = new \app\model\admin\Page();
        $id = $request = $this->request->get('id');
        if(empty($id)){
            $data = array();
        }else{
            $data = $dao->page_detail($id);
        }
        return $this->fetch('page/detail',$data);
    }

    //文章保存修改
    public function page_save()
    {
        $dao = new \app\model\admin\Page();
        $data['id'] = deletehtml($this->request->post('id'));
        $data['title'] = deletehtml($this->request->post('title'));
        $data['keywords'] = deletehtml($this->request->post('keywords'));
        $data['describe'] = deletehtml($this->request->post('describe'));
        $data['content'] = $this->request->post('editorValue');
        $data['read_num'] = deletehtml($this->request->post('read_num'));
        $data['seo_name_en'] = deletehtml($this->request->post('seo_name_en'));
        if(empty($data['id'])){
            $data['add_time'] = strtotime($request = $this->request->post('time'));
            $article_status = $dao->add_page($data);
        }else{
            $data['end_time'] = strtotime($request = $this->request->post('time'));
            $where['id'] = $data['id'];
            $article_status = $dao->update_page($where,$data);
        }
        if($article_status){
            $this->redirect('/admin/page/index?');
        }else{
            $this->error('操作失败！', '/admin/page/detail?id='.$data['id']);
        }
    }

    //文章删除
    public function page_delete()
    {
        $dao = new \app\model\admin\Page();
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $article_delete = $dao->delete_page([$id]);
        if($article_delete == 0){
            return json(['status'=>3,'data'=>(object)[],'msg'=>'删除失败!']);
        }else{
            return json(['status'=>0,'data'=>$article_delete,'msg'=>'操作成功!']);
        }
    }

}


